var SearchItems=function(){
	var curSelect = "n";
	var searchItems = false;
	function initSearch(conf){
		for(var id in conf){
			var $selectItem = $("#"+id);
			var sb = new SearchBtn({
						curSelect:curSelect,
						panelConf:conf[id].panelConf,
						panelEvent:conf[id].panelEvent,
						conf:conf[id],
						selectItem: $selectItem,
						si:this
					});
		}
	}
	return {
		init:function(conf){
			if(searchItems===false){
				initSearch(conf);
				searchItems = true;
			}
		},clearItems:function(){
			searchItems=false;
		}
	};
}();



var SearchBtn = function(conf){
	var si = conf.si;
	var panel = this.panel = null;
	this.$search = $(conf.selectItem);
	this.$btn  = this.$search.children(".select_btn");
	var temp = this;
	this.$btn.click(function(){
		if(temp.isOn()){
			temp.off();
			befor = null;
		}else{
			if(si.curSelect){
				si.curSelect.off();
			}
			si.curSelect = temp;
			temp.panel = panel = SearchPanel.createPanel(conf.panelConf,temp);
			temp.on();
			conf.panelEvent(panel);
		}
	});
}

SearchBtn.prototype = {
	off:function(){
		this.$search.removeClass("on");
		this.panel.close(true);
	},
	on:function(){
		this.$search.addClass("on");
	},
	isOn:function(){
		return this.$search.hasClass("on");
	}
}


var SearchPanel = function(){
	var html=[];
	html[0]='<div id="searchPanel" class="select_search" style="display:block;top:';
	html[1]=0;
	html[2]='px;left:';
	html[3]=0;
	html[4]='px;width:';
	html[5]=0;
	html[6]='px;height:';
	html[7]=0;
	html[8]='px;"><div class="top_border"></div><div class="middle">';
	html[9]='<div class="';
	html[10]='';
	html[11]='">';
	html[12]='';
	html[13]='</div>';
	html[14]='<div class="search_result" style="width:';
	html[15]=0;
	html[16]='%;"><div class="top_border"></div><div class="middle">';
	html[17]='';
	html[18]='</div><div class="bottom_border"></div></div>';
	html[19]='</div><div class="bottom_border"></div></div>';
	function create(conf,btn){
		var $search = $("#Search");
		$search.remove("#searchPanel");
		html[1]=conf.top;
		html[3]=conf.left;
		html[5]=conf.width;
		html[7]=conf.height;
		html[10]=conf.headClass;
		html[12]=conf.head;
		html[15]=conf.resultWidth;
		html[17]=conf.result;
		var h = html.join("");
		$search.append(h);
		this.close = function(bb){
			var sp = $("#searchPanel").get(0);
			if(sp){
				$($search).get(0).removeChild(sp);
			}
			if(!bb){
				btn.off();
			}
		}
	}
	return {
		createPanel:function(conf,btn){
			return new create(conf,btn);
		}
	};
}();


var SearchHandle = function($obj,selects,panel){
	this.$hand = $obj.children(".select_text");
	this.$btn = this.$hand.children(".select_btn");
	this.id = $obj.attr("id");
	var btn = this;
	this.$btn.click(function(){
		var onflag = btn.isOn();
		for(var i=0;i<selects.length;i++){
			selects[i].off();
		}
		if(onflag){
			btn.off();
		}else{
			btn.on();
		}
	});
	if(panel)	this.panel = panel();	
}
SearchHandle.prototype={
	on:function(){
		this.$hand.addClass("on");
		if(this.panel)	this.panel.show(this);
	},
	off:function(flag){
		this.$hand.removeClass("on");
		if(flag===false){
			return;
		}
		if(this.panel)	this.panel.close();
	},
	isOn:function(){
		return this.$hand.hasClass("on");;
	}		
};


//品名搜索
function SearchCommodity(){
	return new CommodityPanel();
}
function CommodityPanel(){
	this.close=function(){
		if(!this.$categoryPanel){
			this.$categoryPanel = $("#commodityPanel");
		}
		this.$categoryPanel.css("display","none");
		if(this.btn) this.btn.off(false);
	}
	this.show=function(btn){
		if(!this.$commodityPanel){
			this.$commodityPanel = $("#commodityPanel");
		}
		var temp = this;		
		this.$commodityPanel.css("display","block");
		$("#commdityMenu").empty();
		$("#commdityMenu").append('<ul class="menuitem"><li><a href="#">是大块发生地方块</a></li><li><a href="#">是大块发生地方块</a></li><li><a href="#">是大块发生地方块</a></li><li><a href="#">是大块发生地方块</a></li></ul>');
		$("#commdityMenu ul li a").click(function(){
			$("#commodityInput").val($(this).text());
			temp.close();
		});
		this.btn = btn;
	}	
}

//搜索，排序，刷新共享
function refreshShareMessage(data){
	$("#shareMessage").append(data);
	
}

//分享按钮事件



//绑定搜索按钮
function bindSearchBtn(){
	$("#searchShare").click(function(){
		var brandval = $("#brand_input").val();
		brandval=brandval =="请选择"?"":brandval;
		var categoryval = $("#categoryInput").val();
		categoryval=categoryval =="请选择"?"":categoryval;
		var commodityval = $("#commodityInput").val();
		commodityval=commodityval =="请选择"?"":commodityval;
		
		LoadShareMessage("shareMessage.html","");
		
	});
}

//加载我的分享
function LoadShareMessage(url,param){
	var $sharemessage = $("#shareMessage");
	$sharemessage.empty();
	$sharemessage.append('<div class="loading"></div>');
	$sharemessage.load(url,param,function(){
		
	});
}

//绑定关闭按钮
function bindClose(){
	$("#closeSharemessage").click(function(){
		$("#myshare").empty();
	$("body").get(0).removeChild($("#shraw").get(0));
	SearchItems.clearItems();
         window.location.reload();
	});
       
}
//绑定我的分享分页按钮
function bindmysharePage(){
	$("#mysharePage span").click(function(){
		LoadShareMessage("shareMessage.html","");
	});
}
